package com.user.dao.impl;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.user.control.createToken;
import com.user.dao.UserDao;
import com.user.domain.LoginInfo;
import com.user.domain.User;

public class UserDaoImpl implements UserDao{
	public SessionFactory sf;
	public Configuration mConfiguration;
	public UserDaoImpl(){
		this.mConfiguration = new Configuration().configure();
		this.sf = mConfiguration.buildSessionFactory();
	}
	@Override
	public boolean save(User u) {
		Session s =sf.openSession();
		try{
			s.beginTransaction();
			s.save(u);
			s.getTransaction().commit();
			s.close();
			return true;
		}catch(Exception e){
			s.close();
			return false;
		}
	}

	@Override
	public User getUser(String str) {
		
		Session s =sf.openSession();
		s.beginTransaction();
		List<User> users = s.createQuery(str).list();
		s.getTransaction().commit();
		if(users.size()==0){
			return new User();
		}else{
			LoginInfo lg = new LoginInfo();
			User u = new User();
			u = users.get(users.size()-1);
			lg.setTellnumber(u.getTellnumber());
			lg.setToken(new createToken().getString());
			s = sf.openSession();	
			s.beginTransaction();
			@SuppressWarnings("unchecked")
			List<LoginInfo> strs = s.createQuery("from LoginInfo where tellnumber='"+u.getTellnumber()+"'").list();
			s.getTransaction().commit();
			s.close();
			if(strs.size()!=0){
				s = sf.openSession();
				s.beginTransaction();
				Query k = s.createQuery("update LoginInfo user set user.token=? where user.tellnumber=?");
				k.setString(0, new createToken().getString());
				k.setString(1, u.getTellnumber());
				k.executeUpdate();
				s.getTransaction().commit();
				
				s.close();
			}else{
				s = sf.openSession();
				s.beginTransaction();
				s.save(lg);
				s.getTransaction().commit();
				s.close();
			}
			return u;
		}
	}

	@Override
	public List<User> getUsers(String str) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session s =sf.openSession();
		s.beginTransaction();
		List<User> users = s.createQuery(str).list();
		s.getTransaction().commit();
		s.close();
		return users;
	}

	@Override
	public boolean updateUser(String tellnumber, String password) {
		Session s =sf.openSession();
		try{
			s.beginTransaction();
//			s.createQuery("update User set password='"+password+"' where tellnumber = '"+tellnumber+"'");
			Query q = s.createQuery("update User user set user.password=? where user.tellnumber=?");
			q.setString(0, password);
			q.setString(1, tellnumber);
			q.executeUpdate();
			s.getTransaction().commit();
			s.close();
			s = sf.openSession();	
			s.beginTransaction();
			@SuppressWarnings("unchecked")
			List<LoginInfo> strs = s.createQuery("from LoginInfo where tellnumber='"+tellnumber+"'").list();
			s.getTransaction().commit();
			s.close();
			if(strs.size()!=0){
				s = sf.openSession();
				s.beginTransaction();
				Query k = s.createQuery("update LoginInfo user set user.token=? where user.tellnumber=?");
				k.setString(0, new createToken().getString());
				k.setString(1, tellnumber);
				k.executeUpdate();
				s.getTransaction().commit();
				s.close();
			}else{
				LoginInfo lg = new LoginInfo();
				lg.setTellnumber(tellnumber);
				lg.setToken(new createToken().getString());
				s = sf.openSession();
				s.beginTransaction();
				s.save(lg);
				s.getTransaction().commit();
				s.close();
			}
			
			return true;
		}catch(Exception e){
			return false;
		}
	}

	@Override
	public String getToken(String str) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session s =sf.openSession();
		s.beginTransaction();
		@SuppressWarnings("unchecked")
		List<LoginInfo> strs = s.createQuery("from LoginInfo where tellnumber='"+str+"'").list();
		s.getTransaction().commit();
		s.close();
		return strs.get(strs.size()-1).getToken();
	}

	@Override
	public boolean checkToken(String str) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session s =sf.openSession();
		s.beginTransaction();
		@SuppressWarnings("unchecked")
		List<LoginInfo> strs = s.createQuery("from LoginInfo where token='"+str+"'").list();
		s.getTransaction().commit();
		s.close();
		if(strs.size()!=0){
			Timestamp d = new Timestamp(System.currentTimeMillis());
			long t = d.getTime()-strs.get(strs.size()-1).getCreatetime().getTime();
			long day = t/(1000*60*60*24);
			if(day>7){
				s =sf.getCurrentSession();
				s.beginTransaction();
				int count = s.createQuery("delete from LoginInfo where token='"+str+"'").executeUpdate();
				s.getTransaction().commit();
				s.close();
				return false;
			}
			return true;
		}
		return false;
	}

}
